﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;

namespace Syspetro.Core.HttpService
{
    public class HttpHelper
    {
        readonly IHttpClientFactory _httpClientFactory;
        public HttpHelper(IHttpClientFactory httpClientFactory)
        {
            this._httpClientFactory = httpClientFactory;
        }
        public Task<HttpResponseMessage> HttpRequestAsync(string url, HttpMethod httpMethod, Dictionary<string, string> FormParameter)
        {
            var (request, client) = SetHttpRequest(url, httpMethod, FormParameter);
            return client.SendAsync(request);
        }
        public HttpResponseMessage HttpRequest(string url, HttpMethod httpMethod, Dictionary<string, string> formParameter)
        {
            var (request, client) = SetHttpRequest(url, httpMethod, formParameter);
            return client.Send(request);
        }
        private (HttpRequestMessage, HttpClient) SetHttpRequest(string url, HttpMethod httpMethod, Dictionary<string, string> formParameter, Action<HttpRequestHeaders> buildHeader = null)
        {
            var request = new HttpRequestMessage(httpMethod, url);
            HttpClient client = new();
            if (buildHeader != null)
            {
                client = BuildClient(url, buildHeader);
            }
            else
            {
                client = _httpClientFactory.CreateClient();
                client.DefaultRequestHeaders.Add("Accept", "text/plain");
            }

            if (formParameter != null)
            {
                request.Content = new FormUrlEncodedContent(formParameter);//x-www-form-urlencoded：会将表单数据转成键/值对进行传递，有大小的限制，一般是2M。
            }
            return (request, client);
        }
        public Task<HttpResponseMessage> HttpRequestAsync(string url, HttpMethod httpMethod, string strParameter)
        {
            var (request, client) = SetHttpRequest(url, httpMethod, strParameter);
            return client.SendAsync(request);
        }
        public HttpResponseMessage HttpRequest(string url, HttpMethod httpMethod, string strParameter)
        {
            var (request, client) = SetHttpRequest(url, httpMethod, strParameter);
            return client.Send(request);
        }
        private (HttpRequestMessage, HttpClient) SetHttpRequest(string url, HttpMethod httpMethod, string strParameter, Action<HttpRequestHeaders> buildHeader = null)
        {
            var request = new HttpRequestMessage(httpMethod, url);
            HttpClient client = new();
            if (buildHeader != null)
            {
                client = BuildClient(url,buildHeader);
            }
            else
            {
                client = _httpClientFactory.CreateClient();
                client.DefaultRequestHeaders.Add("Accept", "text/plain");
            }

            if (strParameter != null)
            {
                request.Content = new StringContent(strParameter, Encoding.UTF8, "application/json");//multipart/form - data:既可以上传键值对，也可以上传文件
            }
            return (request, client);
        }

        private static HttpClient BuildClient(string address, Action<HttpRequestHeaders> buildHeader = null)
        {
            var client = new HttpClient()
            {
                BaseAddress = new Uri(address)
            };

            if (buildHeader == null)
            {
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            }
            else
            {
                buildHeader(client.DefaultRequestHeaders);
            }

            return client;
        }
    }
}
